package com.zhl.microservcie.order.service;

import com.zhl.microservcie.order.pojo.Item;
import com.zhl.microservcie.order.pojo.Order;
import com.zhl.microservcie.order.pojo.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: zhl-microservcie-parent
 * @author: zhl
 * @create: 2019-03-04
 **/
@Service
public class OrderService {

    @Autowired
    private ItemService itemService;

    private static final Map<String, Order> MAP = new HashMap<>();

    static {
        Order order = new Order();
        order.setCreateTime(new Date().getTime());
        order.setUpdateTime(order.getCreateTime());
        order.setUserId(1l);
        order.setOrderId("13452828493");

        List<OrderDetail> list = new ArrayList<>();
        Item item = new Item();
        item.setId(1l);
        list.add(new OrderDetail(order.getOrderId(),item));
        Item item2 = new Item();
        item2.setId(2l);
        list.add(new OrderDetail(order.getOrderId(),item2));

        order.setOrderDetails(list);

        MAP.put(order.getOrderId(), order);

    }

    public Order queryOrderById(String orderId){

        Order order = MAP.get(orderId);
        if (order==null) {
            return null;
        }
        List<OrderDetail> orderDetails = order.getOrderDetails();
        for (OrderDetail orderDetail : orderDetails) {
            Item item = itemService.queryItemById(orderDetail.getItem().getId());
            if (item ==null){
                continue;
            }
            orderDetail.setItem(item);
        }
        return order;
    }
}
